package md.frolov.bible.commands;

import md.frolov.bible.editors.BibleEditorInput;
import md.frolov.bible.editors.MainEditor;
import md.frolov.bible.model.Archive;
import md.frolov.bible.model.Chapter;
import md.frolov.bible.toolbars.NavigationTextContribution;
import md.frolov.bible.views.BooksView;
import md.frolov.bible.views.ModulesView;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IParameter;
import org.eclipse.core.commands.NotEnabledException;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.core.commands.Parameterization;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.handlers.IHandlerService;

public class GoToURLCommand extends AbstractHandler implements IHandler {
	public static final String ID = "md.frolov.bible.commands.GoToURLCommand";
	public static final String URL_PARM_ID = "md.frolov.bible.commands.GoToURLCommand.url";
	
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		String url = event.getParameter(URL_PARM_ID);

		MainEditor me = (MainEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		if(me==null && url==null) {
			OpenEditorCommand.executeStatic(null);
			return null;
		}
		if(me == null) return null;
		if(url == null){
			String yok = NavigationTextContribution.getTextContribution().getText();
			url = ((BibleEditorInput)me.getEditorInput()).getChapter().getBook().getModule().getUrlForStringLocation(yok);
			if(url == null) url = Archive.getInstance().getUrlForStringLocation(yok);
			if(url == null) return null;
		}
		
		me.setBibleInput(new BibleEditorInput(url));
		me.getSite().getPage().activate(me);
		me.setFocus();
		
//		updateNavigationViews(url);
		return null;
	}
	
	public static void updateNavigationViews(String url){
		ModulesView mv = (ModulesView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ModulesView.PLUGIN_ID);
		BooksView bv = (BooksView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(BooksView.PLUGIN_ID);

		Chapter ch = Archive.getInstance().resolveUrl(url);
		
		Object t;
		if((t=bv.getViewer().getSelection())!=null)
			if(t instanceof IStructuredSelection)
				if(ch.equals(((IStructuredSelection)t).getFirstElement())) {
					return;
				}
		
		
		bv.getViewer().setSelection(null);
		mv.getViewer().setSelection(new StructuredSelection(ch.getBook().getModule()));
		bv.getViewer().setSelection(new StructuredSelection(ch));		
	}

	//static calling
	public static void executeStatic(String url){
		try {
			IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
			ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
			
			Command goToCommand = commandService.getCommand(GoToURLCommand.ID);
			IParameter parameter = null;
		
			parameter = goToCommand.getParameter(GoToURLCommand.URL_PARM_ID);
			Parameterization parm = new Parameterization(parameter,url);
			handlerService.executeCommand(new ParameterizedCommand(goToCommand, new Parameterization[]{parm}), null);
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotDefinedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotEnabledException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotHandledException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
